package com.second.oa.service.impl;

import java.sql.SQLException;

import com.second.oa.dao.EmployeeDao;
import com.second.oa.dao.impl.EmployeeDaoImpl;
import com.second.oa.domain.Employee;
import com.second.oa.exception.LoginException;
import com.second.oa.service.EmployeeService;

public class EmployeeServiceImpl implements EmployeeService {

	private EmployeeDao employeeDao = new EmployeeDaoImpl();

	public void add(Employee t) throws SQLException {

	}

	public void update(Employee t) throws SQLException {

	}

	public void delete(String id) throws SQLException {

	}

	/**
	 * 用户登陆
	 * @throws LoginException 
	 * 
	 * @throws SQLException
	 */
	public Employee login(String code, String password) throws LoginException {

		String sql = "select e.id e_id, e.code e_code, e.password e_password, e.name e_name, e.gender e_gender, e.birthday e_birthday, e.depart e_depart, e.job e_job, e.introduction e_introduction, e.phone e_phone, e.email e_email, e.qq e_qq, e.wechat e_wechat, d.id d_id, d.code d_code, d.name d_name, d.remark d_remark, d.parent d_parent from t_employee e  left join t_department d on e.depart = d.id where e.code = ?";

		
		try {
			// 根据账号查询员工信息
			Employee employee = employeeDao.get(sql, code);
			// 判断员工是否存在
			if(employee == null) {
				throw new LoginException("员工不存在.");
			}
			
			// 判断密码是否匹配
			if(!employee.getPassword().equals(password)) {
				throw new LoginException("密码错误.");
			}
			
			// 登陆成功
			return employee;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new LoginException("连接数据库失败,请稍后再试.");
		}
	}

}
